using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Unity.VisualScripting.Member;

public class PlacementState : IBuildingState
{
   
    private ObjectData curSelectDatabase;
    
    
    private Grid grid;
    private PreviewSystem previewSystem;
    private ObjectsDatabaseSO database;
    private GridData floorData;
    private GridData furnitureData;
    private ObjectPlacer objectPlacer;
    private AudioSource soundFeedback;

    public PlacementState( 
                          Grid grid,
                          PreviewSystem previewSystem,
                          ObjectsDatabaseSO database,
                          GridData floorData,
                          GridData furnitureData,
                          ObjectPlacer objectPlacer,
                          AudioSource soundFeedback)
    { 
        this.grid = grid;
        this.previewSystem = previewSystem;
        this.database = database;
        this.floorData = floorData;
        this.furnitureData = furnitureData;
        this.objectPlacer = objectPlacer;
        this.soundFeedback = soundFeedback; 
    }

    public void StartState(int ID)
    {
        int  selectDatabaseIndex = database.objectsData.FindIndex(d => d.ID == ID);
        if (selectDatabaseIndex < 0)
            return;
        curSelectDatabase = database.objectsData[selectDatabaseIndex];

        // cellIndicator.SetActive(true);
        previewSystem.StartShowingPlacementPreview(curSelectDatabase.Prefab,curSelectDatabase.Size); 
        // previewSystem.UpdatePosition(endCellPos,false);
    }

    public void EndState()
    {
        previewSystem.StopShowingPreview();
    }

    public void OnAction(Vector3Int endCellPos)
    {
        // GameObject newObj = GameObject.Instantiate(curSelectDatabase.Prefab);
        // newObj.transform.parent = buildingParent.transform;
        // newObj.transform.position = endCellPos;
        // placeObjList.Add(newObj);
        
        soundFeedback.Play();
        
        int placedObjectIndex = objectPlacer.PlaceObject(curSelectDatabase.Prefab, endCellPos);

        GridData gridData = curSelectDatabase.ID == 0 ? floorData : furnitureData;
        gridData.AddObjectAt(grid.WorldToCell(endCellPos), curSelectDatabase.Size, curSelectDatabase.ID,
            placedObjectIndex);
        
        previewSystem.UpdatePosition(endCellPos,false);
    }

    public bool UpdateState(Vector3Int gridPosition)
    {
        if (curSelectDatabase != null)
        {
            bool ischeckPlace = CheckPlacementValidity(gridPosition, curSelectDatabase);

            // cellIndicatorRenderer.material.color = ischeckPlace ? Color.white : Color.red;
            previewSystem.UpdatePosition(gridPosition,ischeckPlace);

            if (!ischeckPlace)
                return false;
        }
        else
        {
            previewSystem.UpdatePosition(gridPosition,true);
        }

        return true;
    }
    
    private bool CheckPlacementValidity(Vector3Int gridPosition, ObjectData selectedObject) //放置物品 校验
    {
        GridData selectedData = selectedObject.ID == 0 ? floorData : furnitureData;

        return selectedData.CanPlaceObejctAt(gridPosition, selectedObject.Size);
    }
  
   
}

